<?php

namespace app\advertisement\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;

class AdvaccountAbnormalLogic
{

    public function __construct()
    {
        
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $advertiser_id = $params['advertiser_id'] ?? [];
        $platform_id = $params['platform_id'] ?? '';
        $limit = $params['limit'] ?? 10;
        $where = [];

        if(!empty($advertiser_id)){
            $where['a.advertiser_id'] = $advertiser_id;
        }
        if(!empty($platform_id)){
            $where['a.platform_id'] = $platform_id;
        }
        
        $where['a.status'] = 0;
        $list = Db::name('advaccount')
            ->field('a.id,a.advertiser_id,a.advertiser_name,
                e.title as platform_title
            ')
            ->alias('a')
            ->join('qu_advplatform e','e.id = a.platform_id', 'left')
            ->where($where)
            ->paginate($limit);
        $lists = $list->items();
        foreach ($lists as $k=>$v) {
            $advertising = Db::name('advertising')
                ->where('advertiser_id',$v['advertiser_id'])
                ->where('type',2)
                ->order('stat_time desc')
                ->find();
            if(!empty($advertising)){
                $lists[$k]['error'] = $advertising['error'];
                $lists[$k]['stat_time'] = date('Y-m-d', $advertising['stat_time']);
            }
        }
                
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }
    
    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $header = [];
        $header[] = ['平台', 'platform_title'];
        $header[] = ['广告账户', 'advertiser_name'];
        $header[] = ['广告id', 'advertiser_id'];
        $header[] = ['最后更新时间', 'stat_time'];
        $header[] = ['问题详情', 'error'];
        $fileName = "export_Advaccount" . time();

        $advertiser_id = $params['advertiser_id'] ?? [];
        $platform_id = $params['platform_id'] ?? '';
        $where = [];

        if(!empty($advertiser_id)){
            $where['a.advertiser_id'] = $advertiser_id;
        }
        if(!empty($platform_id)){
            $where['a.platform_id'] = $platform_id;
        }
        
        $where['a.status'] = 0;
        $lists = Db::name('advaccount')
            ->field('a.advertiser_id,a.advertiser_name,
                e.title as platform_title
            ')
            ->alias('a')
            ->join('qu_advplatform e','e.id = a.platform_id', 'left')
            ->where($where)
            ->select()->toarray();
        foreach ($lists as $k=>$v) {
            $advertising = Db::name('advertising')
                ->where('advertiser_id',$v['advertiser_id'])
                ->where('type',2)
                ->order('stat_time desc')
                ->find();
            $lists[$k]['error'] = "";
            $lists[$k]['stat_time'] = "";
            $lists[$k]['advertiser_id'] = '"'.$v['advertiser_id'];
            if(!empty($advertising)){
                $lists[$k]['error'] = $advertising['error'];
                if(!empty($advertising['stat_time'])){
                    $lists[$k]['stat_time'] = date('Y-m-d', $advertising['stat_time']);
                }
            }
        }
        
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    //确认
    public function confirm($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        if (empty($id)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        $result = false;

        try {
            
            $update['status'] = 1;
            $result = Db::name('advaccount')
                ->where('id',$data['id'])
                ->update($update);

            if ($result) {
                return json(DefaultResponse(200, false, '操作成功!', null));
            }
            return json(DefaultResponse(201, false, '操作失败!', null));
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '操作失败!', null));
        }
        return json(DefaultResponse(200, false, '操作成功!', null));
    }

}
